package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Description: 定义一个切面
 * User: 故我
 * Date: 2023-11-05
 * Time: 11:33
 */
@Aspect  //定义为一个切面
@Component  //随着类加载启动而启动
public class UserAspect {
    //定义一个切点
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut(){
    }

    //定义通知
    @Before("pointcut()")
    public void BeforeAdvice(){
        System.out.println("执行了 BeforeAdvice 方法!");
    }

    @After("pointcut()")
    public void AfterAdvice(){
        System.out.println("执行了 AfterAdvice 方法!");
    }

    /**
     *  环绕通知
     */
    @Around("pointcut()")
    public Object AroundAdvice(ProceedingJoinPoint joinPoint){
        System.out.println("执行了 AroundAdvice before 方法!");
        Object object = null;
        try {
            object = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("执行了 AroundAdvice after 方法!");
        return object;
    }
}
